package com.qf.sl.youshare;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import com.qf.sl.youshare.adapters.ProductCollectAdapter;
import com.qf.sl.youshare.db.DataManager;
import com.qf.sl.youshare.model.ProductCollect;
import com.qf.sl.youshare.model.UserLogin;
import com.qf.sl.youshare.utils.UserCurrent;

import java.util.List;

/**
 * Created by IDEA
 * User : sl
 * on 2015/2/26.
 * 20:30
 */
public class ProductCollectActivity extends Activity implements View.OnClickListener {

    private List<ProductCollect> productCollects;
    private ProductCollectAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_collect);

        ListView listView = (ListView) findViewById(R.id.product_collect_list);
        if (listView != null) {
            DataManager manager = DataManager.getInstance();

            UserCurrent userCurrent = UserCurrent.getInstance();
            String userId = Constants.DEFAULT_USER_ID;
            UserLogin userLogin = userCurrent.getUserLogin();
            if (userLogin != null) {
                userId = userLogin.getUserId();
            }
            productCollects = manager.getProductCollect(userId);

            adapter = new ProductCollectAdapter(this, productCollects);

            listView.setAdapter(adapter);

            adapter.setClickListener(this);

        }

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        Object tag = v.getTag();
        int position = 0;
        if (tag != null) {
            if (tag instanceof Integer){
                position = (Integer)tag;
            }
        }
        ProductCollect productCollect = productCollects.get(position);
        switch (id){
            case R.id.product_collect_delete:
                String pid = productCollect.getPid();
                String userId = Constants.DEFAULT_USER_ID;
                UserCurrent userCurrent = UserCurrent.getInstance();
                UserLogin userLogin = userCurrent.getUserLogin();
                if (userLogin != null) {
                    userId = userLogin.getUserId();
                }
                DataManager manager = DataManager.getInstance();
                manager.deleteProductFavorite(userId, pid);
                productCollects.remove(position);
                adapter.notifyDataSetChanged();
                break;
        }
    }
}